﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SixApp.Core;
using SixApp.Entities;
using SixApp.Extensions;
using SixApp.Repositories;

namespace SixApp
{
    public partial class FormFormula : BaseForm
    {
        private FormulaRepository mFormulaRepository = new FormulaRepository();

        public DataGridViewRow FormulaCurrentRow
        {
            get
            {
                return this.gridFormula.CurrentRow;
            }
        }

        public string CurrentFormulaType
        {
            get
            {
                return this.tbCurrentFormulaType.Text;
            }
        }

        public FormFormula()
        {
            InitializeComponent();

            gridFormula.AutoGenerateColumns = false;

            BindFormula(string.Empty);
        }

        private void tsbFormula_Click(object sender, EventArgs e)
        {
            int formulaId = 0;
            ShowFormulaCalc(formulaId);
        }

        private void ShowFormulaCalc(int formulaId)
        {
            var formCalc = new FormCalc();
            formCalc.Tag = formulaId;
            formCalc.Owner = this;
            formCalc.ShowDialog();
        }

        private void tsbBest_Click(object sender, EventArgs e)
        {
            BindFormula(((ToolStripButton)sender).Tag.ToString());
        }

        public void BindFormula(string formulaType)
        {
            DataTable dtFormula = mFormulaRepository.GetFormulas(formulaType);
            gridFormula.DataSource = dtFormula;
            tbCurrentFormulaType.Text = formulaType;
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除吗？", "系统信息", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                int formulaId = Convert.ToInt32(gridFormula.CurrentRow.Cells["colId"].Value);
                mFormulaRepository.DeleteFormula(formulaId);

                BindFormula(tbCurrentFormulaType.Text);
            }
        }

        private void miEdit_Click(object sender, EventArgs e)
        {
            int formulaId = Convert.ToInt32(gridFormula.CurrentRow.Cells["colId"].Value);
            ShowFormulaCalc(formulaId);
        }

        private void gridFormula_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int formulaId = Convert.ToInt32(gridFormula.CurrentRow.Cells["colId"].Value);
                ShowFormulaCalc(formulaId);
            }
        }

        private void gridFormula_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            gridFormula.DrawRowNumber(e);
        }   
    }
}
